#include <algorithm>
#include <functional>
#include "Automobile.h"

void Automobile::Attach( IDisplay* obs )
{
	//check if observer is already in the list
	TObserversConstIterator found = std::find(mObservers.begin(), mObservers.end(), obs);
	if (found == mObservers.end()) { //observer not found 
		mObservers.push_back(obs);
	}
}

void Automobile::Detach( IDisplay* obs )
{
	// check if observer is in the list
	TObserversIterator found = std::find(mObservers.begin(), mObservers.end(), obs);
	if (found != mObservers.end()) {
		mObservers.erase(found);
	}
}

void Automobile::Notify() const
{
	// call update on every observer in the list
	TObserversConstIterator it = mObservers.begin();
	while (it != mObservers.end()) {
		(*it)->Update();
		++it;
	}
}